package com.example.devicemanage.controller;

import com.example.devicemanage.entity.Equip;
import com.example.devicemanage.entity.Regist;
import com.example.devicemanage.service.inter.EquipService;
import com.example.devicemanage.service.inter.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class RegisterController {
    @Autowired
    RegisterService registerService;

    @RequestMapping(path = "/register_list", method = RequestMethod.GET)
    public String getRegister_list(HttpServletRequest httpServletRequest) {
        int currentPage = 1;
        int count = 6;
        int totalPage = 0;
        String targetPage = httpServletRequest.getParameter("targetPage");
        if (targetPage != null) {
            currentPage = Integer.parseInt(targetPage);
        }
        List<Regist> list1;
        list1 = registerService.queryAllRegist();
        List<Regist> list = new ArrayList<>();
        if (list1.size() % 6 == 0) {
            totalPage = list1.size() / 6;
        } else {
            totalPage = list1.size() / 6 + 1;
        }
        if (currentPage * count <= list1.size()) {
            for (int i = 0; i < 6; i++) {
                list.add(list1.get((currentPage - 1) * count + i));

            }
        } else {
            int m = list1.size() - (currentPage - 1) * count;
            for (int j = 0; j < m; j++) {
                list.add(list1.get(list1.size() - m + j));
            }
        }


        httpServletRequest.setAttribute("list", list);
        httpServletRequest.setAttribute("totalPage", totalPage);
        httpServletRequest.setAttribute("registNum", list1.size());
        httpServletRequest.setAttribute("currentPage", currentPage);

        return "register_list";
    }

    @RequestMapping(path = "/regist_edit")
    String regist_edit(HttpServletRequest httpServletRequest) {
        String rid = httpServletRequest.getParameter("rid");

        Regist regist = registerService.querySingleEquip(Integer.parseInt(rid));
        httpServletRequest.setAttribute("e", regist);
        return "equip_register";
    }

    @ResponseBody
    @RequestMapping(path = "/regist_save")
    String regist_save(@RequestParam(value = "category") String category, @RequestParam(value = "ename") String ename, @RequestParam(value = "eid") int eid, @RequestParam(value = "model") String model, @RequestParam(value = "spec") String spec, @RequestParam(value = "price") float price, @RequestParam(value = "num") int num, @RequestParam(value = "pdate") String pdate, @RequestParam(value = "manufacturer") String manufacturer, @RequestParam(value = "slife") String slife, @RequestParam(value = "agent") String agent) {
        Regist regist = new Regist();
        regist.setAgent(agent);
        regist.setCategory(category);
        regist.setEid(eid);
        regist.setEname(ename);
        regist.setManufacturer(manufacturer);
        regist.setPdate(pdate);
        regist.setModel(model);
        regist.setNum(num);
        regist.setSlife(slife);
        regist.setSpec(spec);
        regist.setPrice(price);

        return registerService.saveRegist(regist)==1?"ok":"wrong";
    }
    @ResponseBody
    @RequestMapping(path = "/regist_update")
    String regist_update(@RequestParam(value = "rid")int rid,@RequestParam(value = "category") String category, @RequestParam(value = "ename") String ename, @RequestParam(value = "eid") int eid, @RequestParam(value = "model") String model, @RequestParam(value = "spec") String spec, @RequestParam(value = "price") float price, @RequestParam(value = "num") int num, @RequestParam(value = "pdate") String pdate, @RequestParam(value = "manufacturer") String manufacturer, @RequestParam(value = "slife") String slife, @RequestParam(value = "agent") String agent) {

        Regist regist = new Regist();
        regist.setRid(rid);
        regist.setAgent(agent);
        regist.setCategory(category);
        regist.setEid(eid);
        regist.setEname(ename);
        regist.setManufacturer(manufacturer);
        regist.setPdate(pdate);
        regist.setModel(model);
        regist.setNum(num);
        regist.setSlife(slife);
        regist.setSpec(spec);
        regist.setPrice(price);

        return registerService.updateRegist(regist)==1?"ok":"wrong";
    }
    @ResponseBody
    @RequestMapping(path = "/regist_delete")
    String regist_delete(HttpServletRequest httpServletRequest) {
        String rid = httpServletRequest.getParameter("rid");

        Boolean aBoolean = registerService.deleteRegist(Integer.parseInt(rid));

        return aBoolean ? "删除成功" : "删除失败";
    }
    @ResponseBody
    @RequestMapping(path = "/regist_intoEquip")
    String regist_intoEquip(HttpServletRequest httpServletRequest) {
        String rid = httpServletRequest.getParameter("rid");

        Boolean aBoolean = registerService.regist_intoEquip(Integer.parseInt(rid));

        return aBoolean ? "入库成功" : "入库失败,请检查设备id是否冲突";
    }
}
